package com.jdd.modules.device.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdd.common.constant.Send;
import com.jdd.common.util.LoginUserUtils;
import com.jdd.common.util.SnowFlakeUtil;
import com.jdd.feign.MqttFegin;
import com.jdd.modules.device.entity.TDevice;
import com.jdd.modules.device.mapper.TDeviceMapper;
import com.jdd.modules.device.service.TDeviceService;
import com.jdd.modules.mqtt.utils.MqttCmdUtill;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 设备表 服务实现类
 * </p>
 *
 * @author zly
 * @since 2022-03-24
 */

@Service
public class TDeviceServiceImpl extends ServiceImpl<TDeviceMapper, TDevice> implements TDeviceService {

    @Resource
    private MqttFegin feign;

    @Override
    public IPage<TDevice> findAllDevice(Page<TDevice> page, TDevice device, List<String> projectCodeList) {
        IPage<TDevice> completeInfo=baseMapper.findAllDevice(page,device,projectCodeList);
        return completeInfo;
    }

    @Override
    public void openDoor(String deviceSn) {

        JSONObject jsonObject = MqttCmdUtill.openDoor(deviceSn, SnowFlakeUtil.getId());
        Send send = new Send();
        send.setTopic("face/"+deviceSn+"/request");
        send.setCmd(jsonObject.toString());
        send.setCreateUser(LoginUserUtils.getLoginUser().getId());
        send.setMqttType(4);
        feign.sendMessageWithTopic(send);
    }

    @Override
    public void updateDeviceAlias(String deviceParentCode, Integer type,String typeParentCode,String name) {
        /*先判断区域下是否存在设备*/
        LambdaQueryWrapper<TDevice> deviceLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deviceLambdaQueryWrapper.likeRight(TDevice::getParentCode,deviceParentCode);
        List<TDevice> deviceList = this.list(deviceLambdaQueryWrapper);
        /*有设备时在执行此操作，更换设备的别名*/
        if (deviceList.size()>0){
            String parentCode = typeParentCode;
            /*判断parentCode中是否含有区域*/
            String[] as = parentCode.split("A");
            int topAreaNum=0;
            /*含有区域则as集合长度大于1*/
            if (as.length>1){
                /*判断前边有几个父区域*/
                topAreaNum = as.length-1;
            }else{
                topAreaNum = 0;
            }
            for (TDevice tDevice : deviceList) {
                String deviceAlias = tDevice.getDeviceAlias();
                String[] places = deviceAlias.split("-");
                places[topAreaNum+type] = name;
                String newName="";
                for (int i = 0; i < places.length; i++) {
                    if (i>0){
                        newName=newName +"-"+ places[i];
                    }else{
                        newName = newName+places[i];
                    }
                }
                tDevice.setDeviceAlias(newName);
            }
            this.updateBatchById(deviceList);
        }
    }

}
